<template>
  <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="0">
    <el-form-item label="" prop="email">
      <el-input
        placeholder="请输入邮箱"
        autoComplete="on"
        style="position: relative"
        v-model="ruleForm.email"
        @keyup.enter.native="submitForm(ruleFormRef)"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><UserFilled /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input
        placeholder="请输入密码"
        autoComplete="on"
        @keyup.enter.native="submitForm(ruleFormRef)"
        v-model="ruleForm.password"
        :type="passwordType"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><GoodsFilled /></el-icon>
        </template>
        <template #suffix>
          <div class="show-pwd" @click="showPwd"
            ><svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          /></div>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button
        :loading="loading"
        class="login-btn"
        type="primary"
        @click="submitForm(ruleFormRef)"
        >登录</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue'
  import type { FormInstance } from 'element-plus'
  import { ElNotification } from 'element-plus'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/store/modules/user'
  import { getTimeState } from '@/utils/index'
  const ruleFormRef = ref<FormInstance>()
  const router = useRouter()
  const UserStore = useUserStore()

  const passwordType = ref('password')
  const loading = ref(false)
  const rules = reactive({
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
  })
  // 表单数据
  const ruleForm = reactive({
    email: '',
    password: '',
  })

  const showPwd = () => {
    if (passwordType.value === 'password') {
      passwordType.value = ''
    } else {
      passwordType.value = 'password'
    }
  }
  const submitForm = (formEl: FormInstance | undefined) => {
    loading.value = true
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        // 登录
        setTimeout(async () => {
          UserStore.email = ruleForm.email
          const res = await UserStore.loginByPw(ruleForm)
          console.log(res)
          if (res == -1) {
            loading.value = false
            return
          }
          if (UserStore.utype == 3) {
            await router.push({
              path: '/academyDataShow',
            })
          } else {
            await router.push({
              path: '/',
            })
          }
          ElNotification({
            title: getTimeState(),
            message: '欢迎来到智慧党建后台管理系统',
            type: 'success',
            duration: 3000,
          })
          loading.value = true
        }, 1000)
      } else {
        ElNotification({
          title: '警告',
          message: '请输入正确的账号和密码',
          type: 'warning',
          duration: 3000,
        })
        loading.value = false
        return false
      }
    })
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';
</style>
